package cn.chen.proxy.compiler;

import cn.chen.proxy.Moveable;
import cn.chen.proxy.Tank;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;

public class Test1 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String src = "package cn.chen.proxy.compiler;\n" +
                "\n" +
                "import cn.chen.proxy.Moveable;\n" +
                "\n" +
                "public class TankTimeProxy implements Moveable {\n" +
                "\n" +
                "    Moveable tank;\n" +
                "\n" +
                "    public TankTimeProxy(Moveable tank) {\n" +
                "        this.tank = tank;\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public void move() {\n" +
                "        long start = System.currentTimeMillis();\n" +
                "        tank.move();\n" +
                "        long end = System.currentTimeMillis();\n" +
                "        System.out.println(\"运行：\" + (start - end));\n" +
                "    }\n" +
                "}\n";
        // /Users/chenxiaokang/Documents/IDEAProject/mianshi
//        System.out.println(System.getProperty("user.dir"));
        String fileName = System.getProperty("user.dir") + "/src/main/java/cn/chen/proxy/compiler/TankTimeProxy.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write(src);
        fw.flush();
        fw.close();
        // 编译
        try {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            // com.sun.tools.javac.api.JavacTool
    //        System.out.println(compiler.getClass().getName());
            StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null,null,null);
            Iterable units = fileMgr.getJavaFileObjects(fileName);
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileMgr, null, null, null,units);
            task.call();
            fileMgr.close();

            //load into memory and create an instance
            URL[] urls = new URL[]{new URL("file:/" + System.getProperty("user.dir") + "/src/main/java")};
            URLClassLoader ul = new URLClassLoader(urls);
            Class c = ul.loadClass("cn.chen.proxy.compiler.TankTimeProxy");
            System.out.println(c);

            Constructor constructor = c.getConstructor(Moveable.class);
            Moveable m = (Moveable) constructor.newInstance(new Tank());
            m.move();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
